<?php

namespace App\Http\Controllers\admin;

use App\http\model\Article;
use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ArticleController extends CommonController
{
    /**
     * Display a listing of the resource.
     *文章列表  get admin/article
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Article::leftJoin('category', 'article.cate_id', '=', 'category.cate_id')->orderBy('art_id', 'desc')
            ->select('article.*', 'category.cate_name')->paginate(5);
        return view('admin.article.index')->with('data', $data);
    }

    /**
     * Show the form for creating a new resource.
     * 显示文章添加表单  get admin/article/create
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = Category::getlist();
        return view('admin.article.add')->with('data', $data);
    }

    /**
     * Store a newly created resource in storage.
     * 处理文章添加 post admin/article
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store()
    {
        $input = Input::except('_token');
        $rules = [
            'art_title' => 'required',
        ];
        $message = [
            'art_title.required' => '文章标题必须填写!',
        ];
        $validator = Validator::make($input, $rules, $message);
        if ($validator->passes()) {
            $input['art_time'] = time();
            $row = Article::create($input);
            if ($row) {
                return redirect('admin/article');
            } else {
                return back()->with('errors', '添加失败,请稍后重试!');
            }
        } else {
            return back()->withErrors($validator)->withInput();
        }
    }

    /**
     * Display the specified resource.
     *显示单个文章详情 get admin/article/{article}
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *显示修改文章表单 get admin/article/{article}/edit
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($art_id)
    {
        $data = Category::getlist();
        $row = Article::find($art_id);
        return view('admin.article.edit')->with(['data' => $data, 'row' => $row]);
    }

    /**
     * Update the specified resource in storage.
     *处理修改文章表单  put admin/article/{article}
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update($art_id)
    {
        $input = Input::except('_token', '_method');
        $rules = [
            'art_title' => 'required',
        ];
        $message = [
            'art_title.required' => '文章标题必须添加!',
        ];
        $validator = Validator::make($input, $rules, $message);
        if ($validator->passes()) {
            $model = Article::find($art_id);
            $ret = $model->update($input);
            if ($ret) {
                return redirect('admin/article');
            } else {
                return back()->with('errors', '修改失败,请稍后重试!');
            }

        } else {
            return back()->withErrors($validator)->withInput();
        }
    }

    /**
     * Remove the specified resource from storage.
     *删除文章处理  delete admin/article/{article}
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($art_id)
    {
        $ret = Article::where('art_id',$art_id)->delete();
        if($ret){
            $data = [
                'error'=>'0',
                'msg'=>'删除成功!'
            ];
        }else{
            $data = [
                'error'=>'1',
                'msg'=>'删除失败,请稍后再试!'
            ];
        }
        return $data;
    }
}
